#include "Hookers.h"
#include"Kernel.h"
int Hookers::create(char* pathname, unsigned int mode)
{
	User& u = Kernel::Instance().GetUser();
	u.u_dirp = pathname;
	u.u_arg[1] = mode;
	u.u_error = 0;
	Kernel::Instance().getFileManager().Creat();
	if (u.u_error)
	{
		
		return -1;
	}
	else
	{
		return u.u_ar0;//fd
	}
	/*
	char fil[50] = "/kitten";
	theuser->u_dirp = fil;
	theuser->u_arg[1] = (Inode::IRWXU | Inode::IRWXG | Inode::IRWXO);
	thefilemanager->Creat();*/
  
}

int Hookers::createFolders(char* pathname)
{

	User& u = Kernel::Instance().GetUser();
	u.u_dirp = pathname;
	u.u_arg[1] = (Inode::IRWXU | Inode::IRWXG | Inode::IRWXO)|Inode::IFDIR;
	u.u_error = 0;
	Kernel::Instance().getFileManager().Creat();
	if (u.u_error)
	{
		return -1;
	}
	else
	{
		return u.u_ar0;//fd
	}
}

int Hookers::write(int fd, char* source, int slen)
{
	/*
	int fd = theuser->u_ar0;
	char pos[40] = "Carve Your name into the shining stars";
	theuser->u_arg[0] = 0;//fd
	theuser->u_arg[1] = (int)pos;
	theuser->u_arg[2] = strlen(pos);
	thefilemanager->Write();
	*/
	User& u = Kernel::Instance().GetUser();
	u.u_error = 0;
	u.u_arg[0] = fd;
	u.u_arg[1] = int(source);
	u.u_arg[2] = slen;
	Kernel::Instance().getFileManager().Write();
	if (u.u_error)
	{
		return -1;
	}
	else
	{
		return u.u_ar0;
	}

}

int Hookers::close(int fd)
{
	User& u = Kernel::Instance().GetUser();
	u.u_error = 0;
	u.u_arg[0] = fd;
	Kernel::Instance().getFileManager().Close();
	if (u.u_error)
	{
		return -1;
	}
	else
	{
		return u.u_ar0;
	}
	
}

int Hookers::Open(char* pathname, unsigned int mode)
{
	User& u = Kernel::Instance().GetUser();
	u.u_dirp = pathname;
	u.u_arg[1] = mode;
	u.u_error = 0;
	Kernel::Instance().getFileManager().Open();
	if (u.u_error)
	{
		return -1;
	}
	else
	{
		return u.u_ar0;
	}
	/*theuser->u_dirp = fil;
	theuser->u_arg[1] = (Inode::IRWXU | Inode::IRWXG | Inode::IRWXO);
	thefilemanager->Open();
	fd = theuser->u_ar0;*/
	return 0;
}

int Hookers::read(int fd, char* dest, int nbytes)
{
	User& u = Kernel::Instance().GetUser();
	u.u_error = 0;
	u.u_arg[0] = fd;
	u.u_arg[1] = int(dest);
	u.u_arg[2] = nbytes;
	Kernel::Instance().getFileManager().Read();
	if (u.u_error)
	{
		return -1;

	}
	else
	{
		return u.u_ar0;
	}
	/*
	char output[40] = "";
	int len = 38;
	theuser->u_arg[0] = fd;
	theuser->u_arg[1] = int(output);
	theuser->u_arg[2] = len;
	thefilemanager->Read();*/
}

int Hookers::chdir(char* pathname)
{
	User& u = Kernel::Instance().GetUser();
	u.u_dirp = pathname;
	u.u_arg[0] =(int) pathname;
	u.u_error = 0;
	Kernel::Instance().getFileManager().ChDir();
	return u.u_error;
}

void Hookers::stat(char* pathnam, int statbuf)
{
	User& u = Kernel::Instance().GetUser();
	u.u_dirp = pathnam;
	u.u_arg[1] = statbuf;
	u.u_error = 0;
	Kernel::Instance().getFileManager().Stat();
	u.u_error = 0;
}
